package cn.tedu.carehome.foundation.controller;

import cn.tedu.carehome.foundation.pojo.dto.MedicineAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.MedicineUpdateDTO;
import cn.tedu.carehome.foundation.pojo.vo.MedicineListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.MedicineStandardVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.restful.WebConsts;
import cn.tedu.carehome.foundation.service.IMedicineService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/medicines")
@Slf4j
@Api(tags = "药品管理模块")
@Valid
public class MedicineController {
    @Autowired
    private IMedicineService medicineService;

    @PostMapping("/add-new")
    @ApiOperation("添加药品信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@RequestBody MedicineAddNewDTO medicineAddNewDTO) {
        log.debug("开始处理【添加老人健康信息】的请求，参数：{}", medicineAddNewDTO);
        medicineService.addNew(medicineAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除药品信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "药品ID", required = true, example = "3", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000000, message = "删除药品信息失败，药品id非法")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除药品信息】的请求，参数：{}", id);
        medicineService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改药品信息")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@RequestBody MedicineUpdateDTO medicineUpdateDTO) {
        log.debug("开始处理【修改老人健康信息】的请求，参数：{}", medicineUpdateDTO);
        medicineService.updateInfoById(medicineUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询药品信息")
    @ApiOperationSupport(order = 410)
    public JsonResult<MedicineStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询老人健康详情】的请求，ID：{}", id);
        MedicineStandardVO medicineStandardVO = medicineService.getStandardById(id);
        return JsonResult.ok(medicineStandardVO);
    }

    @GetMapping("")
    @ApiOperation("查询药品信息列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<MedicineListItemVO>> list() {
        log.debug("开始处理【查询老人健康信息列表】的请求，无参数");
        List<MedicineListItemVO> list = medicineService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/list-by-page")
    @ApiOperation("分页查询药品信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "page", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "2")
    })
    public JsonResult<JsonPage<MedicineListItemVO>> listMedicinesByPage(
            @RequestParam(required = false, defaultValue = WebConsts.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false, defaultValue = WebConsts.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<MedicineListItemVO> medicinesByPage = medicineService.listMedicinesByPage(page, pageSize);
        return JsonResult.ok(medicinesByPage);
    }
}
